const path = require('path')
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
const resolve = dir => {
  return path.join(__dirname, dir)
}

const projectName = require('./config/projects')
const configObj = require('./config/project.con')
module.exports = {
  lintOnSave: false,
  publicPath: './',
  outputDir: `dist/canteen/`,
  pages: {
    index: {
      entry: `src/projects/wuc/main.js`,
      title: configObj.title,
      template: './public/index.html',
      filename: 'index.html'
    }
  },
  configureWebpack() {
    const devPlugins = [
      /**
       * 缓存加速二次构建速度
       */
      new HardSourceWebpackPlugin(),
      new HardSourceWebpackPlugin.ExcludeModulePlugin([{
        test: /mini-css-extract-plugin[\\/]dist[\\/]loader/
      }])
    ]
    if (process.env.NODE_ENV === 'development') {
      return {
        plugins: devPlugins
      }
    }
  },
  chainWebpack: config => {
    // eslint-disable-next-line no-unused-expressions
    // config
    //   .plugin('html')
    //   .tap(args => {
    //     args[0].title = title
    //     return args
    //   })
    /* 配置svg图标自动加载 begin */
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
    /* 配置svg图标自动加载 end */
    config
      .plugin('define')
      .tap(args => {
        args[0].PROJECT = JSON.stringify(configObj)
        return args
      }), // 定义全局变量
      config.resolve.alias
        .set('@', resolve('src')) // key,value自行定义，比如.set('@@', resolve('src/components'))
        .set('@p', resolve('public'))
        .set('_c', resolve('src/components'))
        .set('#', resolve(`src/projects/${projectName.name}`)) // 定义在每个项目中的src路径
  },
  // 设为false打包时不生成.map文件
  productionSourceMap: false,
  devServer: {
    host: '',
    port: 3030,
    proxy: {
      '^/api': {
        //  target: 'https://league.shunchenghc.com/bizApi/bizunion-server',
        target: 'http://8.148.73.120:9805/bizunion-server',
        //target: 'https://lskj.lskejisoft.com/shujun/canteen-server',
        //target: 'http://59.173.18.239:3011/canteen-server',
        // target: 'http://192.168.5.120:8010',
        // target: 'http://192.168.31.83:8010',
        // target: 'http://192.168.5.48:8010',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      },
    }
  }
}